/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.RemoteException;

/**
 *
 * @author JBP
 */
public class LinuxMonitorImpl extends MonitorImpl implements Monitor{
    
    private Bdd maBdd = new Bdd();
    
    LinuxMonitorImpl() throws RemoteException {

    }

    @Override
    protected Memoire createMemoire(String ip) throws RemoteException {
        return getMemoryLinux(ip);
    }

    private Memoire getMemoryLinux(String ip) throws RemoteException {
        String mem = null;
        Process p = null;
        BufferedReader br = null;

        try {
            //lancement de la commande via ksh, on garde que la memoire
            ProcessBuilder pb = new ProcessBuilder("ksh", "-c", "top -bn 1 | grep Mem:");
            pb.redirectErrorStream(true);
            p = pb.start();

            //recuperation du resultat dans un buffer
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;

            //ajout de chaque ligne dans un string
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                mem = line;
            }
        } catch (Exception x) {
            x.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException ignoreMe) {
                }
            }
            if (p != null) {
                p.destroy();
            }
        }

        //ajout de la memoire libre dans un tableau
        Memoire m = new Memoire();
        m.setFree(mem.substring(42, 49));
        System.out.println("Free Memory : " + m.getFree());

        //ajout de la memoire utilisee dans un tableau
        m.setUsed(mem.substring(27, 34));
        System.out.println("Used Memory : " + m.getUsed());

        //ajout de la memoire totale dans un tableau
        m.setTotal(mem.substring(11, 18));
        System.out.println("Total Memory : " + m.getTotal());

        //Ajout dans la base
        maBdd.addMemoryBDD(ip, m.getFree(), m.getUsed(), m.getTotal());
        return m;
    }

    @Override
    protected Cpu createCpu(String ip) throws RemoteException {
        return getCpuLinux(ip);
    }

    //methode accessible au client Linux pour connaitre le pourcentage de CPU utilise
    public Cpu getCpuLinux(String ip)  throws RemoteException{
        Cpu temp=new Cpu();
     
        Process p = null;
        BufferedReader br = null;
        
        try {
            //lancement de la commande via ksh, on garde que le pourcentage de CPU
            ProcessBuilder pb = new ProcessBuilder("ksh", "-c", "top -bn 1 | grep Cpu");
            pb.redirectErrorStream(true);
            p = pb.start();
            
            //recuperation du resultat dans un buffer
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            
            //ajout de chaque ligne dans un string
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                temp.setCpu(line);
            }
        }
        catch (Exception x) {
            x.printStackTrace();
        }
        finally {
            if (br != null) {try {br.close();} catch (IOException ignoreMe) {}}
            if (p != null) {p.destroy();}
        }
            
            //ajout du pourcentage de CPU utilisé dans un tableau
            temp.setCpu(temp.getCpu().substring(12,14));
            System.out.println("Used CPU : " +temp.getCpu() );
            
            
            //Ajout dans la base
            maBdd.addCpuBDD(ip,temp.getCpu());
            
        return temp; 
    }
    
    @Override
    protected Processus createProcessus(String ip) throws RemoteException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
